'use strict';
const dayjs = require("dayjs");

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;
  
  const MiniProgramUsers = app.model.define(
    'MiniProgramUsers',
    {
      id: { type: INTEGER, primaryKey: true, autoIncrement: true },
      openid: { type: STRING(100), allowNull: false, unique: true },
      unionid: { type: STRING(100) },
      nickname: { type: STRING(50) },
      avatar_url: { type: STRING(255) },
      gender: { type: INTEGER },
      country: { type: STRING(50) },
      province: { type: STRING(50) },
      city: { type: STRING(50) },
      language: { type: STRING(20) },
      phone: { type: STRING(20) },
      student_id: { type: INTEGER },
      status: { type: INTEGER, defaultValue: 1 },
      create_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      },
      update_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      }
    },
    {
      tableName: 'mini_program_users',
      timestamps: true,
      createdAt: 'create_time',
      updatedAt: 'update_time'
    }
  );

  MiniProgramUsers.associate = function() {
    app.model.MiniProgramUsers.belongsTo(app.model.Students, { foreignKey: 'student_id', targetKey: 'id', as: 'student' });
    app.model.MiniProgramUsers.hasMany(app.model.Reservations, { foreignKey: 'user_id', as: 'reservations' });
  };

  return MiniProgramUsers;
};
